/*
 * Copyright (c) 2009, Russell Weir
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package scripting;

class Dictionary {
	private var h : Dynamic;

	/**
	* Creates a new, empty dictionary.
	**/
	public function new() : Void {
		#if neko
		h = untyped __dollar__hnew(0);
		#else
		#error
		#end
	}

	/**
	* Set a value for the given key.
	**/
	public function set( key : Dynamic, value : Dynamic ) : Void {
		untyped __dollar__hset(h, Converter.toNeko(key), Converter.toNeko(value),null);
	}

	/**
	* Get a value for the given key.
	**/
	public function get( key : Dynamic ) : Null<Dynamic> {
		return untyped Converter.toHaxe(__dollar__hget(h, Converter.toNeko(key),null));
	}

	/**
	* Returns [true] if a value exists for the given key, even
	* if the value is [null]
	**/
	public function exists( key : Dynamic ) : Bool {
		return untyped __dollar__hmem(h, Converter.toNeko(key), null);
	}

	/**
	*	Removes a dictionary entry. Returns [true] if
	*	the entry existed.
	**/
	public function remove( key : Dynamic ) : Bool {
		return untyped __dollar__hremove(h,Converter.toNeko(key),null);
	}

	/**
	* Returns an iterator of the dictionary keys.
	**/
	public function keys() : Iterator<Dynamic> {
		var l = new List<Dynamic>();
		untyped __dollar__hiter(h,function(k,_) { l.push(Converter.toHaxe(k)); });
		return l.iterator();
	}

	/**
	* Returns an iterator of the dictionary values.
	**/
	public function iterator() : Iterator<Dynamic> {
		var l = new List<Dynamic>();
		untyped __dollar__hiter(h,function(_,v) { l.push(Converter.toHaxe(v)); });
		return l.iterator();
	}

	public function toString() {
		var s = "{ ";
		var first = true;
		for(key in keys()) {
			if(first) first = false;
			else s += ", ";
			s += Std.string(key) + " => " + Std.string(get(key));
		}
		s += " }";
		return s;
	}
}
